package com.maomao.test.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *
 * @author admin
 * @date 2018/4/24
 */
public class GetChannel {
    private static final int BSIZE = 1024;
    public static void main(String[] args) throws IOException {
        FileChannel fc = new FileOutputStream("C:\\Users\\admin\\Desktop\\selfFile\\test.txt").getChannel();
        fc.write(ByteBuffer.wrap("some text".getBytes()));
        fc.close();
        fc = new RandomAccessFile("C:\\Users\\admin\\Desktop\\selfFile\\test.txt","rw").getChannel();
        fc.write(ByteBuffer.wrap("Some more".getBytes()));
        fc.close();
        fc = new FileOutputStream("C:\\Users\\admin\\Desktop\\selfFile\\test.txt").getChannel();
        ByteBuffer buff = ByteBuffer.allocate(BSIZE);
        fc.read(buff);
        buff.flip();
        while (buff.hasRemaining()) {
            System.out.println((char)buff.get());
        }
    }
}
